package com.ruoyi.factorydata.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.factorydata.domain.ProjectProductionPlan;
import com.ruoyi.factorydata.domain.dto.ProjectProductionPlanDTO;
import com.ruoyi.factorydata.domain.vo.ProjectProductionPlanVo;
import com.ruoyi.factorydata.service.IProjectProductionPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 生产计划Controller
 * 
 * @author ruoyi
 * @date 2025-07-26
 */
@RestController
@RequestMapping("/productionplan/productionplan")
public class ProjectProductionPlanController extends BaseController
{
    @Autowired
    private IProjectProductionPlanService projectProductionPlanService;

    /**
     * 查询生产计划列表
     */
    @PreAuthorize("@ss.hasPermi('productionplan:productionplan:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProjectProductionPlanDTO projectProductionPlanDTO)
    {
        startPage();
        List<ProjectProductionPlanVo> list = projectProductionPlanService.selectProjectProductionPlanVoList(projectProductionPlanDTO);
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setCode(HttpStatus.SUCCESS);
        tableDataInfo.setMsg("查询成功");
        tableDataInfo.setRows(list);
        tableDataInfo.setTotal(new PageInfo(list).getTotal());
        return tableDataInfo;
    }


    /**
     * 导出生产计划列表
     */
    @PreAuthorize("@ss.hasPermi('productionplan:productionplan:export')")
    @Log(title = "生产计划", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProjectProductionPlan projectProductionPlan)
    {
        List<ProjectProductionPlan> list = projectProductionPlanService.selectProjectProductionPlanList(projectProductionPlan);
        ExcelUtil<ProjectProductionPlan> util = new ExcelUtil<ProjectProductionPlan>(ProjectProductionPlan.class);
        util.exportExcel(response, list, "生产计划数据");
    }

    /**
     * 获取生产计划详细信息
     */
    @PreAuthorize("@ss.hasPermi('productionplan:productionplan:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(projectProductionPlanService.selectProjectProductionPlanById(id));
    }

    /**
     * 获取项目产品最新计划
     */
    @GetMapping(value = "/getLastPlanByProjectProductId/{id}")
    public AjaxResult getLastPlanByProjectProductId(@PathVariable("id") Long ProjectProductId){
        ProjectProductionPlan projectProductionPlan = projectProductionPlanService.getLastPlanByProjectProductId(ProjectProductId);
        return success(projectProductionPlan);
    }

    /**
     * 新增生产计划
     */
    @PreAuthorize("@ss.hasPermi('productionplan:productionplan:add')")
    @Log(title = "生产计划", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProjectProductionPlan projectProductionPlan)
    {
        return toAjax(projectProductionPlanService.insertProjectProductionPlan(projectProductionPlan));
    }

    /**
     * 修改生产计划
     */
    @PreAuthorize("@ss.hasPermi('productionplan:productionplan:edit')")
    @Log(title = "生产计划", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProjectProductionPlan projectProductionPlan)
    {
        return toAjax(projectProductionPlanService.updateProjectProductionPlan(projectProductionPlan));
    }

    /**
     * 删除生产计划
     */
    @PreAuthorize("@ss.hasPermi('productionplan:productionplan:remove')")
    @Log(title = "生产计划", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(projectProductionPlanService.deleteProjectProductionPlanByIds(ids));
    }

    @PreAuthorize("@ss.hasPermi('productionplan:productionplan:updateStatusByProjectProductId')")
    @Log(title = "生产计划", businessType = BusinessType.UPDATE)
    @PutMapping("/updateStatusByProjectProductId/{projectProductId}")
    public AjaxResult updateStatusByProjectProductId(@PathVariable Long projectProductId){
        return toAjax(projectProductionPlanService.updateStatusByProjectProductId(projectProductId));
    }

    @Log(title = "生产计划", businessType = BusinessType.UPDATE)
    @PutMapping("/updatePlanedTime")
    public AjaxResult updatePlanedTime(@RequestBody ProjectProductionPlan projectProductionPlan){
        boolean update = projectProductionPlanService.update(new UpdateWrapper<ProjectProductionPlan>().lambda()
                .eq(ProjectProductionPlan::getId, projectProductionPlan.getId())
                .set(ProjectProductionPlan::getPlannedStartTime, projectProductionPlan.getPlannedStartTime())
                .set(ProjectProductionPlan::getPlannedEndTime, projectProductionPlan.getPlannedEndTime())
        );
        return toAjax(update);
    }

    /**
     * 作废计划
     */
    @PreAuthorize("@ss.hasPermi('productionplan:productionplan:updateStatusByProjectProductId')")
    @Log(title = "生产计划", businessType = BusinessType.DELETE)
    @PostMapping("/cancel/{id}")
    public AjaxResult cancel(@PathVariable Long id) {
        return toAjax(projectProductionPlanService.cancel(id,"plain"));
    }
}
